# coding: utf-8
from django.contrib import admin
from django.contrib.auth.models import Group, User
from django.contrib.auth.admin import UserAdmin
from django.contrib.sites.models import Site
from models import *
from forms import CustomUserChangeForm, CustomUserCreationForm

admin.site.unregister(User)
admin.site.unregister(Site)
admin.site.unregister(Group)

def custom_unicode(self):
    return ' '.join([self.first_name, self.last_name])

class CustomUserAdmin(UserAdmin):
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')}
        ),
    )
    form = CustomUserChangeForm
    add_form = CustomUserCreationForm
User.__unicode__ = custom_unicode
admin.site.register(User, CustomUserAdmin)

class BairroAdmin(admin.ModelAdmin):
    list_display = ('nome', 'frete',)
    search_fields = ['nome']
admin.site.register(Bairro, BairroAdmin)

class TelefoneAdmin(admin.ModelAdmin):
    list_display = ('numero', 'dono_do_numero',)
    list_filter = ('usuario',)
    search_fields = ['numero', 'usuario__first_name', 'usuario__last_name']
    def dono_do_numero(self, obj):
        return obj.usuario.get_full_name()
    dono_do_numero.short_description = u'Cliente'
admin.site.register(Telefone, TelefoneAdmin)

class EnderecoAdmin(admin.ModelAdmin):
    list_display = ('logradouro', 'numero', 'complemento', 'bairro', 
        'ponto_ref', 'dono_do_endereco',)
    list_filter = ('bairro', 'usuario',)
    search_fields = ['logradouro', 'bairro__nome', 'usuario__first_name', 
        'usuario__last_name']
    def dono_do_endereco(self, obj):
        return obj.usuario.get_full_name()
    dono_do_endereco.short_description = u'Cliente'
admin.site.register(Endereco, EnderecoAdmin)

class PedidoAdmin(admin.ModelAdmin):
    date_hierarchy = 'data'
    list_display = ('id_printable', 'status', 'data', 'dono_do_pedido', 'valor_total', 
        'valor_pago', 'troco',)
    list_filter = ('status', 'usuario',)
    list_editable = ('status',)
    def id_printable(self, obj):
        return '#%d' % obj.id
    id_printable.short_description = u'Pedido' 
    def dono_do_pedido(self, obj):
        return obj.usuario.get_full_name()
    dono_do_pedido.short_description = u'Cliente'
admin.site.register(Pedido, PedidoAdmin)

class PizzaAdmin(admin.ModelAdmin):
    list_display = ('nome', 'descricao', 'preco_p', 'preco_m', 'preco_g',)
    search_fields = ['descricao']
admin.site.register(Pizza, PizzaAdmin)

class PizzaPedidaAdmin(admin.ModelAdmin):
    list_display = ('sabores', 'tamanho', 'quantidade', 'pedido_printable',)
    search_fields = ['id']
    def sabores(self, obj):
        return ', '.join([p.nome for p in obj.pizza.all()])
    def pedido_printable(self, obj):
        return u'#%d' % obj.pedido.id
    pedido_printable.short_description = u'Pedido'
    pass
admin.site.register(PizzaPedida, PizzaPedidaAdmin)

class CategoriaAdmin(admin.ModelAdmin):
    list_display = ('nome',)
    search_fields = ['nome']
admin.site.register(Categoria, CategoriaAdmin)

class ItemAdmin(admin.ModelAdmin):
    list_display = ('nome', 'preco', 'categoria')
    list_filter = ('categoria',)
    search_fields = ['nome', 'categoria__nome']
admin.site.register(Item, ItemAdmin)

class ItemPedidoAdmin(admin.ModelAdmin):
    list_display = ('item', 'quantidade', 'pedido_printable',)
    search_fields = ['id']
    def pedido_printable(self, obj):
        return u'#%d' % obj.pedido.id
    pedido_printable.short_description = u'Pedido'
admin.site.register(ItemPedido, ItemPedidoAdmin)
